<?php 
require_once '../3rd_party/phpword/PHPWord.php';
$PHPWord = new PHPWord();
$tmpl_n = $tmpl . '_n';
$templates = $orm->get('pb_colg_template', NULL, NULL, NULL, NULL, NULL)->d;
if (count($templates) > 0 && file_exists($templates[0]->$tmpl)) {
  $doc = $PHPWord->loadTemplate($templates[0]->$tmpl);
  
  $opts = new stdClass();
  $opts->cd = 't.id = :v1';
  $opts->cdv = array(':v1' => $tmpl_id);
  $opts->sel = 't2.formal_name AS __formal_name, t.*, t1.first_name AS __first_name, t1.last_name AS __last_name, t1.id_number AS __id_number, t1.date_of_birth AS __date_of_birth, t1.gender AS __gender';
  $opts->jt = 'LEFT JOIN pb_colg_student AS t1 ON t1.id = t.student_id LEFT JOIN pb_colg_program AS t2 ON t2.id = t.program_id';
  $data = $orm->get('pb_colg_enrollment', NULL, NULL, NULL, NULL, $opts)->d;
  $data[0]->program_name = $data[0]->__formal_name;
  
  $rslt = $orm->get('pb_colg_intake', 't.start', 'ASC', NULL, NULL, NULL)->d;
  $data[0]->__intakes = '';
  $data[0]->__itknum = 0;
  foreach ($rslt as $itm) {
    if (strpos($data[0]->intakes__, "," . $itm->id . ",") === false) {
      continue;
    }
    $data[0]->__itknum++;
    $data[0]->__intakes .= (($data[0]->__intakes == '') ? '' : '<w:br/>') . (($data[0]->program_type == PT_DIPLOMA) ? "Intake " : "Term ") . $data[0]->__itknum . ": $itm->start to $itm->end";
  }
  
  $opts = new stdClass();
  $opts->cd = 't.enrollment_id = :v1';
  $opts->cdv = array(':v1' => $data[0]->id);
  $pays = $orm->get('pb_colg_payment', NULL, NULL, NULL, NULL, $opts)->d;
  
  $bpts = unserialize(BPTS);
  
  $obj = clone $data[0];
  foreach ($obj as $key => $val) {
    $new_key = '__' .  $key;
    $data[0]->$new_key = $val;
    if (isset($bpts[$key])) {
      foreach ($pays as $itm) {
        if ($itm->particular == $bpts[$key]) {
          $data[0]->$new_key = $itm->amount;
        }
      }
    }
  }
  
  $data[0]->__gender = $data[0]->__gender == 0 ? 'Male' : 'Female';
  $data[0]->fee_enrollment = sprintf("%.2f", $data[0]->fee_exam + $data[0]->fee_registration + $data[0]->fee_material);
  if ($data[0]->program_type == PT_NCEA) {
    $data[0]->fee_enrollment = sprintf("%.2f", $data[0]->fee_enrollment + $data[0]->fee_admin);
  }
  $data[0]->total = sprintf("%.2f", $data[0]->fee_homestay + $data[0]->fee_admin + $data[0]->fee_pickup + $data[0]->fee_living + $data[0]->fee_other + $data[0]->fee_enrollment + $data[0]->total_accommodation + $data[0]->total_insurance + $data[0]->total_academic);
  if ($data[0]->program_type == PT_NCEA) {
    $data[0]->total = sprintf("%.2f", $data[0]->total - $data[0]->fee_admin);
  }
  
  $data[0]->__fee_enrollment = sprintf("%.2f", $data[0]->__fee_exam + $data[0]->__fee_registration + $data[0]->__fee_material);
  if ($data[0]->program_type == PT_NCEA) {
    $data[0]->__fee_enrollment = sprintf("%.2f", $data[0]->__fee_enrollment + $data[0]->__fee_admin);
  }
  $data[0]->__total = sprintf("%.2f", $data[0]->__fee_homestay + $data[0]->__fee_admin + $data[0]->__fee_pickup + $data[0]->__fee_living + $data[0]->__fee_other + $data[0]->__fee_enrollment + $data[0]->__total_accommodation + $data[0]->__total_insurance + $data[0]->__total_academic);
  if ($data[0]->program_type == PT_NCEA) {
    $data[0]->__total = sprintf("%.2f", $data[0]->__total - $data[0]->__fee_admin);
  }
  
  $tmpl_cd = Utils::get_param('cd', '');
  if ($tmpl_cd == '') {
    $data[0]->condition_ln1 = '';
    $data[0]->condition_ln2 = '';
  } else {
    $data[0]->condition_ln1 = 'CONDITIONS: This offer is conditional upon the following:';
    $data[0]->condition_ln2 = $tmpl_cd;
  }
  ____setup($doc, $data[0]);
  
  $tmp = '../tmp/' . Utils::unique() . '.docx';
  $doc->save($tmp);
  
  $name = pathinfo($templates[0]->$tmpl_n, PATHINFO_FILENAME);
  $ext = pathinfo($templates[0]->$tmpl_n, PATHINFO_EXTENSION);
  header('Content-Disposition: attachment; filename="' . $name . '#' . $data[0]->__id_number . '.' . $ext . '"');
  readfile($tmp); // or echo file_get_contents($temp_file);
  unlink($tmp);  // remove temp file
}
?>